package com.tomcov.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tomcov.client.service.UserService;
import com.tomcov.shared.UserInfo;

//http://code.google.com/intl/fr/appengine/docs/java/datastore/jdo/queries.html

public class UserServiceImpl extends RemoteServiceServlet implements UserService{

	private static final long serialVersionUID = 1L;

	public UserInfo login(String username, byte[] pass){
		String passdec="";
		/*try{
			Cryptage crypt=new Cryptage();
			passdec=crypt.Decrypt(pass);
		}
		catch(Exception e){
			
		}*/
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName == userNameParam && password== passParam");
		query.declareParameters("String userNameParam, String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(username,passdec);
			if(results.size()==0){
				return null;
			}
			else{UserInfo user= results.get(0);
				SessionHandler sessionh=new SessionHandler();
				HttpServletRequest request = this.getThreadLocalRequest();
				sessionh.setUserInSession(request,user.getId());
				return  user;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	
	public UserInfo login(String username, String pass){
		
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName == userNameParam && password== passParam");
		query.declareParameters("String userNameParam, String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(username,pass);
			if(results.size()==0){
				return null;
			}
			else{UserInfo user= results.get(0);
				SessionHandler sessionh=new SessionHandler();
				HttpServletRequest request = this.getThreadLocalRequest();
				sessionh.setUserInSession(request,user.getId());
				return  user;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	public UserInfo addUser(UserInfo user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query= pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName == userNameParam");
		query.declareParameters("String userNameParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(user.getUserName());
			if(results.size()==0)	
				{pm.makePersistent(user);
				return user;}
			else{
				return null;
				}
			}
		finally{
			query.closeAll();
			pm.close();
		}
		
	}

	public UserInfo getUserByName(String username){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName==userNameParam");//+" && "+"pass==passParam");
		query.declareParameters("String userNameParam");//,String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(username);//,pass);
			if(results.size()==0){
				return null;
				}
			else{UserInfo user= results.get(0);
			return user;}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public UserInfo getUserInSession(){
		SessionHandler sessionh=new SessionHandler();
		HttpServletRequest request = this.getThreadLocalRequest();
		String Id = sessionh.getUserInSessionById(request);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("id==userNameParam");//+" && "+"pass==passParam");
		query.declareParameters("String userNameParam");//,String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(Id);//,pass);
			if(results.size()==0){
				return null;
				}
			else{UserInfo user= results.get(0);
			return user;}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public UserInfo getUserById(String Id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("id==userNameParam");
		query.declareParameters("String userNameParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(Id);
			if(results.size()==0){
				return null;
				}
			else{UserInfo user= results.get(0);
			return user;}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public UserInfo updateUserInfo(UserInfo user, UserInfo modify) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	UserInfo e = pm.getObjectById(com.tomcov.shared.UserInfo.class, user.getId());
	            e.setFirstName(modify.getFirstName());
	            e.setLastName(modify.getLastName());
	            e.setEmailAddress(modify.getEmailAddress());
	            e.setPassword(modify.getPassword());
	            e.setLangage(modify.getLangage());
	            e.setAge(modify.getAge());
	            e.setCons(modify.getCons());
	            e.setMark(modify.getMark());
	            e.setModel(modify.getModel());
	            e.setPhone(modify.getPhone());
	            e.setPlace(modify.getPlace());
	            
	            return modify;

	    } finally {
	        pm.close();
	    }
	}

	@Override
	public Integer logoutUserInSession() {
		HttpServletRequest request = this.getThreadLocalRequest();
		request.getSession(false).invalidate();
		if(request.getSession(false)==null){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	
	

	
}


